using System;
using System.Ext.Xml;

namespace mfXmlSerializer
{
    class mfSerializationMapCreationIterator
    {

        public delegate mfMapBaseElement delCreateSpecificElement(mfMapBaseElement mapElement, int depth, InspectParameter para);

        public static void IterateThroughMap(mfSerializationMap map, delCreateSpecificElement inspectElementAction, InspectParameter para)
        {
            IterateThroughMapInternal(map, inspectElementAction, 0, para);
        }

        private static void IterateThroughMapInternal(mfSerializationMap map
            , delCreateSpecificElement inspectElementAction
            , int depth
            , InspectParameter para)
        {
            for(int i =0;i< map.SerializationElements.Count;i++)
            {
                mfMapBaseElement element = map.SerializationElements[i];

                map.SerializationElements[i] = InspectElement(inspectElementAction, element, depth, para);
            }
        }

        private static mfMapBaseElement InspectElement(delCreateSpecificElement inspectElementAction, mfMapBaseElement element, int depth, InspectParameter para)
        {
            var specificElement = inspectElementAction.Invoke(element, depth, para);

            if (element.HasChildMap)
            {
                depth++;
                IterateThroughMapInternal(element.ChildMap, inspectElementAction, depth, para);
            }

            return specificElement;
        }
    }
}
